package com.jbp.service.impl;

import com.jbp.exception.RRException;
import com.jbp.model.Room;
import com.jbp.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import com.jbp.dao.RoomUserWheatDao;
import com.jbp.model.RoomUserWheat;
import com.jbp.service.RoomUserWheatService;


@Service
public class RoomUserWheatServiceImpl extends ServiceImpl<RoomUserWheatDao, RoomUserWheat> implements RoomUserWheatService {


    @Autowired
    private RoomService roomService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RoomUserWheat> page = this.page(
                new Query<RoomUserWheat>().getPage(params),
                new QueryWrapper<RoomUserWheat>()
        );
        return new PageUtils(page);
    }

    @Override
    public void presenter(RoomUserWheat roomUserWheat) {
        Room room=roomService.getById(roomUserWheat.getRoomId());
        if (room == null){
            throw  new RRException("房间信息错误");
        }
        /**
         * 查看该房间主持人是否存在
         */
        RoomUserWheat userWheat=this.getOne(new QueryWrapper<RoomUserWheat>().eq("room_wheat",1));
        if (userWheat != null){
            userWheat.setRoomWheat(null);
            this.updateById(userWheat);
        }
        roomUserWheat.setRoomWheat(1);
        roomUserWheat.setType("common");
        roomUserWheat.setCreateTime(new Date());
        roomUserWheat.setStatus("online");
        this.saveOrUpdate(roomUserWheat);
    }

    @Override
    public List<Integer> getUserIds(Integer roomId) {
        return baseMapper.getUserIds(roomId);
    }

    @Override
    public List<RoomUserWheat> notEmptyRooms() {
        return baseMapper.notEmptyRooms();
    }

    @Override
    public List<Integer> allUserIds() {
        return baseMapper.allUserIds();
    }
}
